<?php
namespace app\index\model;
use \think\Session;
use think\Model;
use think\Db;
class Users extends Model
{
    protected $table = 'bl_admin';
    protected $rule = [
        'uid'  =>  '',
    ];
    protected $msg = [
        'uid'  =>  '',
    ];
    protected $return = array();
    //获取用户信息
    public function getList($id){
        $where = array();
        $where ['id'] = $id;
        $userInfo = $this->getInfo($where);
        return $userInfo;
    }
    public function checkPwd($data){
        $condition = array(
            'id' => Session::get("adminId")
        );
        $user_info = $this->getInfo($condition, $field = '*');
        if (authcode($user_info['password'], 'DECODE') != $data['old_password']) {
            $this->return ['status'] = 0;
            $this->return ['msg'] = '原密码输入错误';
        } else {
            $data ['password'] = authcode($_POST['new_password'], 'ENCODE');
            $res = $this->save($data,$condition);
            if($res){
                $this->return ['status'] = 1;
                $this->return ['msg'] = '修改成功';
            }else{
                $this->return ['status'] = 0;
                $this->return ['msg'] = '修改失败';
            }
        }
        return $this->return;
    }

    // 获取用户信息

    public function getUserList()
    {
        $result = Db::name('users')->alias('u')
                    ->join('invest i','u.level = i.id','left')
                    ->join('balance b','b.user_id = u.user_id','left')
                    ->where('u.user_id',session('uid'))->field('u.*,i.*,b.id,b.balance,b.earnings,b.jetton')->find();
        if($result['pid'] != 0){
        $puser = Db::name('users')->where(['user_id'=>$result['pid']])->find();
        // print_r($puser);die;
        $result['puser_name'] = $puser['user_name'];
        }else{
            $result['puser_name'] = "";
        }
        // print_r($result);die;
        return $result;
    }

    // 获取下级会员信息

    public function getSubordinate($id)
    {
        $where = "";
        $where["pid"] = $id;

        $result = Db::name('users')->alias('u')
                  ->join('invest i','u.level = i.id','left')
                  ->where($where)->where('level > 1')->select();
        return $result;
    }

    /**
     * 修改用户信息
     */
    public function edit_user($data)
    {
        $id = $data['user_id'];
        $result = Db::name('users')->where('user_id',$id)->update($data);
        if($result){
            return AjaxReturn(1,lang("set_success_edit")); 
        }else{
            return AjaxReturn(0,lang("set_error_edit")); 
        }
    }

    /**
     * 用户-注册会员
     */
    public function add_register_vip($data)
    {
        // 查询用户是否存在
        $users = Db::name('users')->where('user_name',$data['user_name'])->find();
        $res = Db::name('users')->where('user_id',session('uid'))->find();
        if($users){
            return AjaxReturn(0,lang('reg_false_user_name'));
        }
        $add['user_name'] = $data['user_name'];
        $add['user_tel'] = $data['user_tel'];
        $add['real_name'] = $data['real_name'];
        $add['pid'] = session('uid');
        $add['path'] = $res['path'].$res['user_id'].",";
        $add['user_pwd'] = md5('123456');
        $add['status'] = 0;
        $add['create_time'] = time();
        Db::startTrans();
        $result = Db::name('users')->insert($add);
        $id=Db::getLastInsID();
        $res1=Db::name("balance")->insert(["user_id"=>$id]);
        $res2=Db::name("balance_system")->insert(["user_id"=>$id]);
        if($result&&$res1&&$res2){
            Db::commit();
            return AjaxReturn(1,lang('reg_success'));
        }else{
            Db::rollback();
            return AjaxReturn(0,lang('reg_error'));
        }
    }

    /**
     * 修改用户密码
     */

    public function edit_pwd($data){
        $encrypt['user_id'] = session('uid');
        $encrypt['encrypt'] = $data['question'];
        $encrypt['answer'] = $data['encrypted_answer'];
        $res = Db::name('encrypt_answer')->insert($encrypt);
        if($res){
            //修改用户密码
            $result = Db::name('users')->where('user_id',session('uid'))->update(['user_pwd'=>md5($data['newpass'])]);
            if($result){
                return AjaxReturn(1,lang("set_success_edit")); 
            }else{
                return AjaxReturn(0,lang("set_error_edit")); 
            }
        }
    }

    public function next_level($id)
    {
        $result = Db::name('users')->alias('u')
        ->join('balance b','b.user_id = u.user_id','left')
        ->join('invest i','i.id = u.level','left')
        ->where('pid',$id)->select();
        return $result;
    }
}
